package com.wyh.controller;

import com.wyh.Mapper.PlanMapper;
import com.wyh.pojo.Plan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
public class PlanController {
    @Autowired
    PlanMapper planMapper;


    @RequestMapping("/plans")
    public String list(Model model){
        List<Plan> all = planMapper.getPlanList();

        model.addAttribute("plans",all);
        return "plan/list";
    }
    @GetMapping("/plan")
    public String toAddPage(Model model){
        return "plan/add";
    }

    @PostMapping("/plan")
    public String addPlan(Plan plan){
        planMapper.addPlan(plan);
        return "redirect:/plans";
    }

    @GetMapping("/toEditPlan/{id}")
    public String toEdit(@PathVariable("id") Integer id, Model model){
        Plan plan = planMapper.getPlanById(id);
        model.addAttribute("plan",plan);
        return "plan/update";
    }

    @PostMapping("/updatePlan")
    public String updatePlan(Plan plan){
        planMapper.updatePlan(plan);
        return "redirect:/plans";
    }

    @GetMapping("/deletePlan/{id}")
    public String delete(@PathVariable("id") Integer id){
        planMapper.deletePlan(id);
        return "redirect:/plans";
    }

    @RequestMapping("/searchPlan")
    public String searchPlan(String name, Model model){
        Plan planList = planMapper.getPlanByName(name);
        model.addAttribute("plans",planList);
        return "plan/list";
    }
}
